import { dictionaryData } from "@/data/dictionary.js";
/**
 * 防抖
 * @param {*} fn
 * @param {*} t
 */
export const Debounce = (fn, t) => {
  const delay = t || 500;
  let timer;
  return function() {
    const args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      fn.apply(this, args);
    }, delay);
  };
};

/*
 * 函数节流
 */
export const Throttle = (fn, t) => {
  let last;
  let timer;
  const interval = t || 500;
  return function() {
    const args = arguments;
    const now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(() => {
        last = now;
        fn.apply(this, args);
      }, interval);
    } else {
      last = now;
      fn.apply(this, args);
    }
  };
};

/**
 *根据name 获取字典对应的文字
 * @param {*} type 字典类型
 * @param {*} name 字典类型name
 */
export const getDictionaryName = (type, name) => {
  const dictionaryList = dictionaryData[type] || [];
  const rs = dictionaryList.filter(item => {
    return item.value === name;
  });
  return rs[0] || { label: "---" };
};

/**
 * 根据字典类型 获取字典集合
 * @param {*} type
 */
export const getDictionary = type => {
  const dictionaryList = dictionaryData[type] || [];
  return dictionaryList;
};
